import Dialog from '../../dist/dialog/dialog';

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showMore: app.globalData.showMore,
    id: null,
    userInfo: {},
    list: [],
    mine: false,
    status:''
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.toggle("id", options.id);

  },
  remove(event){
    var actid = event.currentTarget.dataset.actid, 
        userid = event.currentTarget.dataset.userid,
        that = this;
    Dialog.confirm({
      message: "确定要将该好友移出本活动吗?"
    }).then(() => {
      app.req({
        url: "wechat/act/participation/exitByManager",
        method: "post",
        data: {
          activityId: actid,
          ids: [userid]
        },
        success(res) {
          console.log(res)
          if (res.code == 200) {
            wx.showToast({
              title: '报名人移除成功',
              icon: 'none',
              duration: 2000
            })
            that.loadListData();
          } else {
            wx.showToast({
              title: res.msg,
              icon: 'none',
              duration: 2000
            })
          }
        }
      });
    }).catch(() => {
      
    });
    
  },
  loadListData: function() {
    var that = this;
    app.req({
      url:"wechat/activity/activityDetail",
      method:"post",
      data:{
        id:that.data.id,
      },
      success:function(res){
        if(200 == res.code){
          console.log("结束", res)
          that.setData({
            status: res.data.status
          })
        }
      }
    })
    app.req({
      url: "wechat/act/participation/listActParticipantByActId",
      method: "post",
      data: {
        activityId: that.data.id,
        isCheck: 1
      },
      success: function(res) {
        if (200 == res.code) {
          var list = res.data;
          var userId = that.data.userInfo.id;
          for (var i = 0; i < list.length; i++) {
            var itemVar = list[i];
            if (itemVar.participantId == userId) {
              if (itemVar.isCreate || itemVar.isManage) {
                that.toggle("mine", true);
              }
            }
          }
          that.toggle("list", list);
        }else {
            wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 2000
            })
        }
      }
    });
  },
  toManagePage: function() {
    var that = this;
    wx.navigateTo({
      url: '/pages/listregistermanage/listregistermanage?id=' + that.data.id
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    app.getUserInfo(function(userInfo) {
      that.toggle("userInfo", userInfo);
      that.loadListData();
    });

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.loadListData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.loadListData();
  },
})